package com.yuyou.fn.controller;


import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.vo.TeacherQueryVo;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.report.vo.ClassLectureDetailQueryVo;
import com.yuyou.fn.util.Webs;
import com.yuyou.fn.wage.entity.WageCoefficientPerformanceHistory;
import com.yuyou.fn.wage.service.IWageCoefficientPerformanceHistoryService;
import com.yuyou.fn.wage.service.IWageCoefficientPerformanceService;
import com.yuyou.fn.wage.service.IWageTeacherDollarsService;
import com.yuyou.fn.wage.vo.CoefficientPerformanceUpdateVo;
import com.yuyou.fn.wage.vo.EmployeeCoefficientPerformance;
import com.yuyou.fn.wage.vo.TeacherDollarsDetailVo;
import com.yuyou.fn.wage.vo.TeacherDollarsUpdateVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * Created by itachi on 2017/04/05.
 */
@Controller
@RequestMapping("/api/admin/wage")
public class AdminWageController extends ApiBaseController {

    @Resource
    private IWageCoefficientPerformanceService wageCoefficientPerformanceService;

    @Resource
    private IWageCoefficientPerformanceHistoryService wageCoefficientPerformanceHistoryService;

    @Resource
    private IWageTeacherDollarsService wageTeacherDollarsService;


    @ApiOperation("绩效系数列表")
    @RequestMapping(value = "/findTeacherCoefficientPerformancePage",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<EmployeeCoefficientPerformance>> findTeacherCoefficientPerformancePage(TeacherQueryVo teacherQueryVo,@RequestParam("pageNo")int pageNo,@RequestParam("pageSize")int pageSize, HttpServletRequest request){
        DataPermission dataPermission=this.getDataPermission(request);
        /*外部兼职的老师*/
        teacherQueryVo.setJobNature(1);
        Page<EmployeeCoefficientPerformance> teacherCoefficientPerformancePage = wageCoefficientPerformanceService.findTeacherCoefficientPerformancePage(dataPermission, teacherQueryVo, pageNo, pageSize);
        return ReturnResult.success(teacherCoefficientPerformancePage);
    }

    @ApiOperation("导出绩效系数列表")
    @RequestMapping(value = "/exportTeacherCoefficientPerformance",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> exportTeacherCoefficientPerformance(TeacherQueryVo teacherQueryVo, HttpServletRequest request,HttpServletResponse response) throws Exception{
        DataPermission dataPermission=this.getDataPermission(request);
        /*外部兼职的老师*/
        teacherQueryVo.setJobNature(1);
        Webs.writeExcel(request,response, wageCoefficientPerformanceService.exportTeacherCoefficientPerformance(dataPermission, teacherQueryVo), "绩效系数列表");
        return ReturnResult.success("ok");
    }

/*    @ApiOperation("单个绩效系数")
    @RequestMapping(value = "/findTeacherCoefficientPerformance",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<EmployeeCoefficientPerformance> findTeacherCoefficientPerformance(@RequestParam(value = "employeeId")Long employeeId, HttpServletRequest request){
        EmployeeCoefficientPerformance teacherCoefficientPerformance = wageCoefficientPerformanceService.findTeacherCoefficientPerformance(employeeId);
        return ReturnResult.success(teacherCoefficientPerformance);
    }*/


    @ApiOperation("绩效系数工资账号编辑保存")
    @RequestMapping(value = "/updateEmployeeCoefficientPerformance",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> updateEmployeeCoefficientPerformanceSetting(@ModelAttribute CoefficientPerformanceUpdateVo coefficientPerformanceUpdateVo , HttpServletRequest request){
        wageCoefficientPerformanceService.updateEmployeeCoefficientPerformanceSetting(coefficientPerformanceUpdateVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("绩效系数历史记录")
    @RequestMapping(value = "/findHistoryByEmployeeId",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<WageCoefficientPerformanceHistory>> findHistoryByEmployeeId(@RequestParam("employeeId")Long employeeId,@RequestParam("pageNo")int pageNo,@RequestParam("pageSize")int pageSize, HttpServletRequest request){
        Page<WageCoefficientPerformanceHistory> performanceHistoryPage = wageCoefficientPerformanceHistoryService.findCoefficientPerformanceHistoryByEmployeeId(employeeId, pageNo, pageSize);
        return ReturnResult.success(performanceHistoryPage);
    }

/*    @ApiOperation("绩效系数excel导入")
    @RequestMapping(value = "/importTeacherCoefficientPerformance",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> importTeacherCoefficientPerformance( HttpServletRequest request,@RequestParam(value = "file") MultipartFile file) throws Exception{
        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();
        List<ImportTeacherCoefficientPerformanceVo> importTeacherCoefficientPerformanceVoList = ExcelImportUtil.importExcel(inputStream, ImportTeacherCoefficientPerformanceVo.class, params);
        wageCoefficientPerformanceService.importTeacherCoefficientPerformanceSetting(importTeacherCoefficientPerformanceVoList);
        return ReturnResult.success("ok");
    }*/

    @ApiOperation("兼职信息列表")
    @RequestMapping(value = "/findTeacherDollarsDetailList",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<TeacherDollarsDetailVo>> findTeacherDollarsDetailList(@ApiParam("发薪月份")@RequestParam("payMonth")String payMonth,@ApiParam("不选不传，0未确认，1确认")@RequestParam(value = "confirmStatus",required = false) Integer confirmStatus, @ModelAttribute ClassLectureDetailQueryVo classLectureDetailQueryVo, HttpServletRequest request) throws Exception{
        DataPermission dataPermission=this.getDataPermission(request);
        classLectureDetailQueryVo.setJobNature(1);
        classLectureDetailQueryVo.setStatuses(Arrays.asList(1,3));
        Date realMonth= TimeUtils.changeMonth(Utils.parseDate(payMonth,"yyyy-MM"),-1);
        classLectureDetailQueryVo.setStartClassDate(TimeUtils.firstDateOfMonth(realMonth));
        classLectureDetailQueryVo.setEndClassDate(TimeUtils.lastDateOfMonth(realMonth));
        List<TeacherDollarsDetailVo> teacherDollarsDetailList = wageTeacherDollarsService.findTeacherDollarsDetailListByPermission(payMonth, dataPermission, classLectureDetailQueryVo, confirmStatus);
        return ReturnResult.success(teacherDollarsDetailList);
    }

    @ApiOperation("导出兼职信息列表")
    @RequestMapping(value = "/exportWageTeacherDollars",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> exportWageTeacherDollars(@ApiParam("发薪月份")@RequestParam("payMonth")String payMonth, @ApiParam("不选不传，0未确认，1确认")@RequestParam(value = "confirmStatus",required = false) Integer confirmStatus, @ModelAttribute ClassLectureDetailQueryVo classLectureDetailQueryVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        DataPermission dataPermission=this.getDataPermission(request);
        classLectureDetailQueryVo.setJobNature(1);
        classLectureDetailQueryVo.setStatuses(Arrays.asList(1,3));
        Date realMonth= TimeUtils.changeMonth(Utils.parseDate(payMonth,"yyyy-MM"),-1);
        classLectureDetailQueryVo.setStartClassDate(TimeUtils.firstDateOfMonth(realMonth));
        classLectureDetailQueryVo.setEndClassDate(TimeUtils.lastDateOfMonth(realMonth));
        Webs.writeExcel(request,response, wageTeacherDollarsService.exportWageTeacherDollars(payMonth, dataPermission, classLectureDetailQueryVo, confirmStatus), "兼职信息列表");
        return ReturnResult.success("ok");
    }

    @ApiOperation("编辑兼职信息")
    @RequestMapping(value = "/updateTeacherDollars",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> updateTeacherDollars(@ModelAttribute TeacherDollarsUpdateVo teacherDollarsUpdateVo, HttpServletRequest request) throws Exception{
        wageTeacherDollarsService.updateTeacherDollars(teacherDollarsUpdateVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("确认兼职信息")
    @RequestMapping(value = "/confirmTeacherDollars",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> confirmTeacherDollars(@RequestParam("teacherDollarsIds")String teacherDollarsIds, HttpServletRequest request) throws Exception{
        wageTeacherDollarsService.confirmTeacherDollars(Utils.toLongIds(teacherDollarsIds));
        return ReturnResult.success("ok");
    }

    @ApiOperation("撤回确认的兼职信息")
    @RequestMapping(value = "/cancelTeacherDollars",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> cancelTeacherDollars(@RequestParam("teacherDollarsIds")String teacherDollarsIds, HttpServletRequest request) throws Exception{
        wageTeacherDollarsService.cancelTeacherDollars(Utils.toLongIds(teacherDollarsIds));
        return ReturnResult.success("ok");
    }

}
